home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14614 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  61 lines

  1. Path: CS.Arizona.EDU!not-for-mail
  2. From: kdb@CS.Arizona.EDU (Koen De Bosschere)
  3. Newsgroups: comp.lang.c++
  4. Subject: C++ meta-problem
  5. Date: 31 Mar 1996 14:32:32 -0700
  6. Organization: University of Arizona CS Department, Tucson AZ
  7. Message-ID: <4jmtlg$smm@baskerville.CS.Arizona.EDU>
  8. NNTP-Posting-Host: baskerville.cs.arizona.edu
  9.  
  10. In order to prevent to write an iterator for every operation 
  11. I want to perform on a list, I tried to write a kind of 
  12. meta-iterator that would apply a particular method to 
  13. all listnodes. For some reason, I cannot execute a 
  14. function variable on an object. Does anyone has an
  15. idea how I can solve this problem?
  16.  
  17. Thanks.
  18.  
  19. -- Koen De Bosschere
  20.  
  21. Test program:
  22.  
  23. #include <iostream.h>
  24.  
  25. class testclass {
  26. public:
  27.   testclass(long d) { data = d; }
  28.   ~testclass() {} 
  29.   void print() { cout << data << endl; }
  30.   testclass *getnext() { return next; }
  31. private:
  32.   long data;
  33.   testclass *next;
  34. };
  35.  
  36. // The next function applies the function "f" to all
  37. // the nodes starting at listnode "root".
  38.  
  39. void iterate(testclass *root, void (testclass::*f)())
  40. {
  41.   testclass *p = root;
  42.   while (p) { 
  43.     p->f();      //  <--- here the compiler generates an error (see below)
  44.     p = p->getnext();
  45.   }
  46. }
  47.  
  48. main()
  49. {
  50.   testclass *root;
  51. //  ....
  52.   iterate(root, testclass::print);
  53. //  ....
  54. }
  55.  
  56.  
  57. Compiler (g++) error:
  58.  
  59. testclass.cc: In function `void iterate(class testclass *, void (testclass::*)())':
  60. testclass.cc:18: no member function `testclass::f()' defined
  61.